package com.test.nnooolwdeaer.dialog_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.adapter_wooden.InfoInputListAdapter_wooden;
import com.test.nnooolwdeaer.util_wooden.address.AddressUtil_Viet_wooden;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.Arrays;
import java.util.List;


public class AddressSelectDialog_Viet_wooden extends BottomSheetDialog {

    private Context context_wooden;

    private TextView titleTv_wooden;
    private ListView listView_wooden;

    private TextView textView_wooden;

    private StringBuilder addressStrB_wooden = new StringBuilder();

    private String[] addressStrs_wooden = new String[2];

    private final OnAddressCheckFinishListener onAddressCheckFinishListener_wooden;

    public AddressSelectDialog_Viet_wooden(@NonNull Context context, TextView textView, OnAddressCheckFinishListener onAddressCheckFinishListener) {
        super(context,R.style.BottomSheetDialogStyle);
        this.context_wooden = context;
        this.textView_wooden = textView;
        this.onAddressCheckFinishListener_wooden = onAddressCheckFinishListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address_select_wooden);
        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initView_wooden();
    }

    @SuppressLint({"SetTextI18n", "ResourceAsColor"})
    private void initView_wooden(){

        titleTv_wooden = findViewById(R.id.dialog_address_select_tv_title);
        listView_wooden = findViewById(R.id.dialog_address_select_lv_list);

        findViewById(R.id.dialog_address_select_ib_close).setOnClickListener(v -> dismiss());

        //选择州
        titleTv_wooden.setText("Tỉnh");
        List<String> stateList = Arrays.asList(AddressUtil_Viet_wooden.getInstance_wooden().getStates_wooden());
        listView_wooden.setAdapter(new InfoInputListAdapter_wooden(getContext(),stateList));

        listView_wooden.setOnItemClickListener((parent, view, position, id) -> {
            //保存州
            String state = stateList.get(position);
            addressStrB_wooden.append(state).append(" ");
            addressStrs_wooden[0] = state;

            //选择市
            titleTv_wooden.setText("Thành phố");
            List<String> cityList =Arrays.asList(AddressUtil_Viet_wooden.getInstance_wooden().getCities_wooden(state));
            listView_wooden.setAdapter(new InfoInputListAdapter_wooden(getContext(),cityList));

            listView_wooden.setOnItemClickListener((parent1, view1, position1, id1) -> {

                //保存市
                String city = cityList.get(position1);
                addressStrB_wooden.append(city);
                addressStrs_wooden[1] = city;

                //写入地址
                textView_wooden.setText(addressStrB_wooden.toString());

                onAddressCheckFinishListener_wooden.onFinish(addressStrs_wooden);

                dismiss();

            });

        });
    }

    public interface OnAddressCheckFinishListener{
        void onFinish(String[] addressStrs);
    }
}
